package com.ruoyi.crm.service.impl;

import java.util.List;
import com.ruoyi.common.core.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.core.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.crm.domain.CustomerProduct;
import com.ruoyi.crm.mapper.PaymentMapper;
import com.ruoyi.crm.domain.Payment;
import com.ruoyi.crm.service.IPaymentService;

/**
 * 付款单Service业务层处理
 *
 * @author ruoyi
 * @date 2021-09-01
 */
@Service
public class PaymentServiceImpl implements IPaymentService
{
    @Autowired
    private PaymentMapper paymentMapper;

    /**
     * 查询付款单
     *
     * @param id 付款单主键
     * @return 付款单
     */
    @Override
    public Payment selectPaymentById(String id)
    {
        return paymentMapper.selectPaymentById(id);
    }

    /**
     * 查询付款单列表
     *
     * @param payment 付款单
     * @return 付款单
     */
    @Override
    public List<Payment> selectPaymentList(Payment payment)
    {
        return paymentMapper.selectPaymentList(payment);
    }

    /**
     * 新增付款单
     *
     * @param payment 付款单
     * @return 结果
     */
    @Transactional
    @Override
    public int insertPayment(Payment payment)
    {
        int rows = paymentMapper.insertPayment(payment);
        insertCustomerProduct(payment);
        return rows;
    }

    /**
     * 修改付款单
     *
     * @param payment 付款单
     * @return 结果
     */
    @Transactional
    @Override
    public int updatePayment(Payment payment)
    {
        payment.setUpdateTime(DateUtils.getNowDate());
        paymentMapper.deleteCustomerProductByCustomerName(payment.getId());
        insertCustomerProduct(payment);
        return paymentMapper.updatePayment(payment);
    }

    /**
     * 批量删除付款单
     *
     * @param ids 需要删除的付款单主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deletePaymentByIds(String[] ids)
    {
        return paymentMapper.deletePaymentByIds(ids);
    }

    /**
     * 删除付款单信息
     *
     * @param id 付款单主键
     * @return 结果
     */
    @Override
    public int deletePaymentById(String id)
    {
        paymentMapper.deleteCustomerProductByCustomerName(id);
        return paymentMapper.deletePaymentById(id);
    }

    /**
     * 新增顾客产品信息信息
     *
     * @param payment 付款单对象
     */
    public void insertCustomerProduct(Payment payment)
    {
        List<CustomerProduct> customerProductList = payment.getCustomerProductList();
        String id = payment.getId();
        if (StringUtils.isNotNull(customerProductList))
        {
            List<CustomerProduct> list = new ArrayList<CustomerProduct>();
            for (CustomerProduct customerProduct : customerProductList)
            {
                customerProduct.setCustomerName(id);
                list.add(customerProduct);
            }
            if (list.size() > 0)
            {
                paymentMapper.batchCustomerProduct(list);
            }
        }
    }
}